package com.timoon.webcomponent.utility.product.single;

import org.apache.wicket.Component;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.sample.dao.model.Product;
import com.timoon.webcomponent.utility.common.HorizontalTab4Panel;
import com.timoon.webcomponent.utility.common.HorizontalTab4Parameter;
import com.timoon.webcomponent.utility.product.BaseProductPanel;
import com.timoon.webcomponent.utility.product.single.component.ProductCommentListPanel;
import com.timoon.webcomponent.utility.product.single.component.ProductDetailTabPanel;
import com.timoon.webcomponent.utility.product.single.component.ProductPackListPanel;
import com.timoon.webcomponent.utility.product.single.component.ProductServiceAfterSalePanel;

/**
 *<p>显示一个商品详细界面</p>
 *<p>PageParameters:product_id</p>
 *<p>public</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */

public class SingleProductPanel extends BaseProductPanel {

	@SpringBean(name="com.timoon.webcomponent.utility.product.service.ProductService")
	private SingleProductService service;
	
	private PageParameters pageParameters;
	
	private Product product;
	
	public SingleProductPanel(String id,PageParameters pageParameters) {
		super(id);
		// TODO Auto-generated constructor stub
		if(pageParameters.get("product_id").isNull()){
			throw new RuntimeException("缺失product_id参数");
		}
		
		this.pageParameters=pageParameters;
		
		this.product=this.service.loadProductById(this.pageParameters.get("product_id").toInt());
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		renderPic();
		
		renderBaseProduct();
		
		renderTabs();
	}

	private void renderTabs() {
		// TODO Auto-generated method stub
		
		add(new AjaxLazyLoadPanel("wk_tabs") {
			
			@Override
			public Component getLazyLoadComponent(String arg0) {
				// TODO Auto-generated method stub
				HorizontalTab4Parameter parameter=new HorizontalTab4Parameter();
				parameter.setLabels(new String[]{"商品介绍","包装清单","商品评价","售后保障"});
				parameter.setPanels(new Panel[]{
						new ProductDetailTabPanel("wk_inner1",product.getId(),3),
						new ProductPackListPanel("wk_inner2",product),
						new ProductCommentListPanel("wk_inner3",product.getId()),
						new ProductServiceAfterSalePanel("wk_inner4", product)
				});
				
				return new HorizontalTab4Panel(AjaxLazyLoadPanel.LAZY_LOAD_COMPONENT_ID, parameter);
			}
		});
	}

	private void renderBaseProduct() {
		// TODO Auto-generated method stub
		add(new Label("wk_name",product.getName()));
	}

	private void renderPic() {
		// TODO Auto-generated method stub
		add(new Image("wk_img",new PackageResourceReference(this.getClass(),"name.jpg")));
	}

	public SingleProductService getService() {
		return service;
	}

	public void setService(SingleProductService service) {
		this.service = service;
	}

}
